<template>
  <div class="app-container">
    <embed :src="src" style="padding:0px;" width="100%" height="100%">
  </div>

</template>
<style>
.app_img_icon::before{font-size: 100px;}
</style>
<script>

// import pdf from 'vue-pdf'
import axios from 'axios'
import request from '@/utils/request'

export default {

  data() {
    return {
      src: ''
    }
  },
  created() {
    this.getPdfCode()
  },
  methods: {
    vf() {
      var data = {}
      return new Promise(resolve => {
        request({
          url: '/pdf.xinjiapo',
          method: 'get',
          responseType: 'blob',
          data
        }).then(res => {
        // str是后台返回的文件名称 放在了response header里
          // console.log(res)
          const blob = new Blob([res.data], {
            type: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
          })
          if (window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveBlob(blob)
          } else {
            const elink = document.createElement('a')
            elink.download = 'test'
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click()
            document.body.removeChild(elink)
          }
          resolve('导出报表成功！')
        })
      })
    },

    // pdf() {
    //   pdf().then(res => {
    //     console.log(res)
    //   })
    // },
    getPdfCode() {
      axios({
        method: 'post',
        url: 'http://www.hjd.com/pdf.xinjiapo',
        headers: {
          'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        },
        responseType: 'blob' // 设置响应的数据类型为一个包含二进制数据的 Blob 对象，必须设置！！！
      }).then(response => {
        console.log(response)
        this.src = this.getObjectURL(response.data)
      }).catch(function(error) {
        console.log(error)
      })
    },

    // 将返回的流数据转换为url
    getObjectURL(file) {
      let url = null
      if (window.createObjectURL !== undefined) { // basic
        url = window.createObjectURL(file)
      } else if (window.webkitURL !== undefined) { // webkit or chrome
        try {
          url = window.webkitURL.createObjectURL(file)
        } catch (error) {
          console.log(error)
        }
      } else if (window.URL !== undefined) { // mozilla(firefox)
        try {
          url = window.URL.createObjectURL(file)
        } catch (error) {
          console.log(error)
        }
      }
      return url
    }

  }

}
</script>
